//
//  GameController.m
//  MyPopStar
//
//  Created by tarena on 16/1/6.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#define fistViewTOPPre 0.0703
#define fistViewHightPre 0.03125
#define fistViewWightPre 0.6
#import "GameController.h"
#import "ImgLabView.h"
#import "PauseView.h"
#import "GameManager.h"
@interface GameController ()<GameManagerDelegate>
//---------界面相关--------
@property (nonatomic,strong) UIImageView *backgroundView;//背景
//-----
@property (nonatomic,strong) UIView *firstView;//头部
@property (nonatomic,strong) UIImageView *bestScoreView;
@property (nonatomic,strong) ImgLabView *bestScoreLab;//最高分
@property (nonatomic,strong) UIButton *btnPause;//暂停
//-----
@property (nonatomic,strong) UIView *secondView;//头部
@property (nonatomic,strong) UIImageView *stageView;
@property (nonatomic,strong) ImgLabView *stageLab;//关数
@property (nonatomic,strong) UIImageView *targetView;
@property (nonatomic,strong) ImgLabView *targetLab;//目标分数
//-----
@property (nonatomic,strong) UIImageView *scoreView;
@property (nonatomic,strong) ImgLabView *nowScoreLab;//当前分数
//-----
@property (nonatomic,strong) PauseView *PauseView;//暂停界面
//--------------------------
@property (nonatomic,strong) GameManager *gameManager;//游戏控制器
@property (nonatomic,weak) StarGroupView *starsView;//游戏界面
@property (nonatomic,assign) StartType type;//游戏进入状态

@end

@implementation GameController
#pragma mark --初始化

-(instancetype)init{
    if (self=[super init]) {
        self.type = StartTypeNew;
    }
    return self;
}
-(instancetype)initWithResume{
    if (self=[super init]) {
        self.type = StartTypeResum;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self mInit];

}
-(void)mInit{
    [self backgroundView];
    [self mInitFirstView];
    [self mInitSecondView];
    [self mInitThirdView];
    [self gameManager];
    [self starsView];
//    [self.gameManager startWithType:self.type];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.gameManager startWithType:self.type];
}
-(void)mInitFirstView{
    //初始化fisrView;
    [self firstView];
    //添加左边图片
    [self bestScoreView];
    //中间面板
    [self bestScoreLab];
    //暂停按钮
    [self btnPause];
    
}
-(void)mInitSecondView{
    //初始化 secondView
    [self secondView];
    [self stageView];
    [self stageLab];
    [self targetView];
    [self targetLab];
}
-(void)mInitThirdView{
    [self scoreView];
    [self nowScoreLab];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}
- (void)dealloc
{
    MyLog(@"%@ -- dealloc",[self class]);
    [self.gameManager over];
}

#pragma mark ---代理

-(void)GameManager:(GameManager *)manager{
}
-(void)GameManager:(GameManager *)manager GameOverStatus:(GameStatus)status{
    if (status==GameStatusPass) {
        [self.gameManager startWithType:(StartTypePass)];
    }else{
        self.PauseView.hidden=NO;
    }
}
-(void)GameManager:(GameManager *)manager GameLevel:(NSInteger)level{
    [self.stageLab ChangeTitle:level];
}
-(void)GameManager:(GameManager *)manager GameScore:(NSInteger)score{
    [self.nowScoreLab ChangeTitle:score];
}
-(void)GameManager:(GameManager *)manager GameTargetScore:(NSInteger)score{
    [self.targetLab ChangeTitle:score];
}

-(void)GameManager:(GameManager *)manager bestScore:(NSInteger)bestScore{
    [self.bestScoreLab ChangeTitle:bestScore];
}
-(void)bestScore:(NSInteger)integer{
    [self.bestScoreLab ChangeTitle:integer];
}

#pragma mark --Pause点击事件
-(void)clickPause:(UIButton *)btn{
    self.PauseView.hidden=!self.PauseView.isHidden;
}

#pragma mark --懒加载
- (UIImageView *)backgroundView {
    if(_backgroundView == nil) {
        _backgroundView = [[UIImageView alloc] init];
        _backgroundView.image = [UIImage imageNamed:@"bg_main"];
        [self.view addSubview:_backgroundView];
        WeakSelf(content);
        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(content.view);
        }];
    }
    return _backgroundView;
}

#pragma mark ---头部firstView
- (UIView *)firstView {
	if(_firstView == nil) {
		_firstView = [[UIView alloc] init];
        [self.view addSubview:_firstView];
        
        [_firstView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(viewHeight *fistViewTOPPre);
            make.height.mas_equalTo(fistViewHightPre *viewHeight);
            make.left.mas_equalTo(0.02*viewWidth);
            make.right.mas_equalTo(-0.02*viewWidth);
        }];
    }
	return _firstView;
}

- (UIImageView *)bestScoreView {
    if(_bestScoreView == nil) {
        _bestScoreView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Best"]];
        _bestScoreLab.contentMode = UIViewContentModeScaleAspectFit;
        [self.firstView addSubview:_bestScoreView];
        [_bestScoreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.top.bottom.left.mas_equalTo(0);
            make.width.mas_equalTo(viewWidth*0.344);
            
        }];
    }
    return _bestScoreView;
}

- (ImgLabView *)bestScoreLab {
	if(_bestScoreLab == nil) {
		_bestScoreLab = [[ImgLabView alloc] initWithBackgroundImg:[UIImage imageNamed:@"ui3"] andTitle:0];
        [self.firstView addSubview:_bestScoreLab];
        WeakSelf(content);
        [_bestScoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.top.bottom.mas_equalTo(0);
            make.width .mas_equalTo(0.389*viewWidth);
            
            make.left.mas_equalTo(content.bestScoreView.mas_right).mas_equalTo(0.056*viewWidth);
        }];
	}
	return _bestScoreLab;
}

- (UIButton *)btnPause {
	if(_btnPause == nil) {
		_btnPause = [[UIButton alloc] init];
        [_btnPause setBackgroundImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
        [_btnPause addTarget:self action:@selector(clickPause:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.firstView addSubview:_btnPause];
        WeakSelf(content);
        [_btnPause mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.width.mas_equalTo(fistViewHightPre * viewHeight);
            make.top.bottom.mas_equalTo(0);
            make.left.mas_equalTo(content.bestScoreLab.mas_right).mas_equalTo(0.056*viewWidth);
        }];
        
	}
	return _btnPause;
}

#pragma mark ---头部secondView
- (UIView *)secondView {
	if(_secondView == nil) {
		_secondView = [[UIView alloc] init];
        [self.view addSubview:_secondView];

        WeakSelf(content);
        [_secondView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(content.firstView.mas_bottom).mas_equalTo(viewHeight*fistViewTOPPre/4);
            make.size.mas_equalTo(content.firstView).mas_equalTo(0);
            make.left.right.mas_equalTo(content.firstView).mas_equalTo(0);
        }];
	}
	return _secondView;
}

- (UIImageView *)stageView {
    if(_stageView == nil) {
        _stageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Stage"]];
        _stageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.secondView addSubview:_stageView];
        
        [_stageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(viewWidth *0.183);
        }];
    }
    return _stageView;
}

- (ImgLabView *)stageLab {
	if(_stageLab == nil) {
		_stageLab = [[ImgLabView alloc] initWithBackgroundImg:[UIImage imageNamed:@"ui2"] andTitle:1];
        [self.secondView addSubview:_stageLab];
        WeakSelf(content);
        [_stageLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(viewWidth*0.12);
            
            make.left.mas_equalTo(content.stageView.mas_right).mas_equalTo(20);
        }];
	}
	return _stageLab;
}

- (UIImageView *)targetView {
	if(_targetView == nil) {
		_targetView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Target"]];
        _targetView.contentMode = UIViewContentModeScaleAspectFit;
        [self.secondView addSubview:_targetView];
        WeakSelf(content);
        [_targetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(viewWidth *0.222);
            
            make.left.mas_equalTo(content.stageLab.mas_right).mas_equalTo(20);
        }];
	}
	return _targetView;
}

- (ImgLabView *)targetLab {
	if(_targetLab == nil) {
		_targetLab = [[ImgLabView alloc] initWithBackgroundImg:[UIImage imageNamed:@"ui2"] andTitle:0];
        [self.secondView addSubview:_targetLab];
        WeakSelf(content);
        [_targetLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            
            make.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(viewWidth*0.278);
            
            make.left.mas_equalTo(content.targetView.mas_right).mas_equalTo(viewWidth*0.05);
        }];
        
	}
	return _targetLab;
}

#pragma mark ---头部thirdView;

- (UIImageView *)scoreView {
	if(_scoreView == nil) {
		_scoreView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Score"]];
        _scoreView.contentMode = UIViewContentModeScaleAspectFit;
        
        [self.view addSubview:_scoreView];
        WeakSelf(content);
        [_scoreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(content.secondView.mas_bottom).mas_equalTo(viewHeight*0.035*2);
            make.height.mas_equalTo(viewHeight*0.032);
        }];
        
	}
	return _scoreView;
}

- (ImgLabView *)nowScoreLab {
	if(_nowScoreLab == nil) {
		_nowScoreLab = [[ImgLabView alloc] initWithBackgroundImg:[UIImage imageNamed:@"ui1"] andTitle:0];
        [self.view addSubview:_nowScoreLab];
        WeakSelf(content);
        [_nowScoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(content.scoreView.mas_bottom).mas_equalTo(viewHeight*0.035/2);
            make.width.mas_equalTo(viewWidth*0.38);
            make.height.mas_equalTo(viewHeight*0.035);
        }];
	}
	return _nowScoreLab;
}
#pragma mark ---暂停界面
- (PauseView *)PauseView {
	if(_PauseView == nil) {
        WeakSelf(content);
		_PauseView = [[PauseView alloc] initWithPauseBlock:^(PauseType type) {
            if (PauseTypeResume==type ) {//从新开始
                [content.gameManager startWithType:StartTypeNew];
                content.PauseView.hidden=!content.PauseView.isHidden;
            }else if(PauseTypeSaveExit == type){//保存退出
                [self.gameManager saveData];
                [content dismissViewControllerAnimated:YES completion:nil];
            }else{
                content.PauseView.hidden=YES;
            }
        }];
        
        _PauseView.hidden=YES;
        [self.view addSubview:_PauseView];
        [_PauseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.width.mas_equalTo(0.534*viewWidth);
            make.height.mas_equalTo(0.0703125*viewHeight*3);
        }];
	}
	return _PauseView;
}

- (GameManager *)gameManager {
	if(_gameManager == nil) {
		_gameManager = [[GameManager alloc] init];
        self.gameManager.delegate=self;
	}
	return _gameManager;
}

- (StarGroupView *)starsView {
	if(_starsView == nil) {
		_starsView = [self.gameManager getGameView];
        [self.view addSubview: self.starsView];
        [self.starsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_offset(0);
            make.height.mas_equalTo(viewWidth);
        }];
	}
	return _starsView;
}

@end
